﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SmartCity.Shared;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace CommonManagement
{
    /// <summary>
    /// 新闻评论应用服务类
    /// </summary>
    public class CommentAppService : ApplicationService, ICommentAppService
    {
        private readonly IAuthManager _authManager;
        private readonly CommentManager _commentManager;
        private readonly IRepository<Comment, int> _commentRepository;

        public CommentAppService(
            IAuthManager authManager,
            CommentManager commentManager,
            IRepository<Comment, int> commentRepository)
        {
            _authManager = authManager;
            _commentManager = commentManager;
            _commentRepository = commentRepository;
        }

        // 分页列表
        public async Task<ListResponse<CommentDto>> PagedListAsync(PagedListRequest request,
            int newsId, int? userId = null, DateTime? commentDate = null)
        {
            // 数据筛选
            var query = _commentRepository
                .Include(m => m.User)
                .Include(m => m.Press)
                .AsQueryable();
            query = query.Where(m => m.NewsId == newsId);
            query = query.WhereIf(userId.HasValue, m => m.UserId == userId.Value);
            query = query.WhereIf(commentDate.HasValue, m => m.CommentDate > commentDate.Value && m.CommentDate < commentDate.Value.AddDays(1));
            // 计数
            var count = query.Count();
            // 排序
            if (!string.IsNullOrWhiteSpace(request.OrderByColumn))
            {
                var orderColumns = request.OrderByColumn.Split(',');
                foreach (var column in orderColumns)
                {
                    query = query.OrderBy(column);
                }
            }
            // 分页
            query = query.Page(request.PageNum, request.PageSize);
            // 返回结果
            var list = await query.ToListAsync();
            var dtos = ObjectMapper.Map<List<Comment>, List<CommentDto>>(list);
            return ListResponse<CommentDto>.Successful(count, dtos);
        }

        // 详情
        public async Task<ResultResponse<CommentDto>> GetAsync(int id)
        {
            var query = _commentRepository
                .Include(m => m.User)
                .Include(m => m.Press)
                .AsQueryable();
            var entity = await query.FirstOrDefaultAsync(m => m.Id == id);
            var dto = ObjectMapper.Map<Comment, CommentDto>(entity);
            return ResultResponse<CommentDto>.Successful(dto);
        }

        // 创建
        public async Task<ActionResponse> CreateAsync(CommentCreateDto input)
        {
            // 读取用户Id
            var userId = _authManager.GetUserId();
            // 新增
            await _commentManager.CreateAsync(
                input.AppType,
                input.NewsId,
                userId,
                input.Content);
            // 返回结果
            return ActionResponse.Successful();
        }

        // 点赞
        public async Task<ActionResponse> LikeAsync(int id)
        {
            var entity = await _commentRepository.GetAsync(id);
            entity.Like();
            // 返回结果
            return ActionResponse.Successful();
        }
    }
}